﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using SqlMiM.Common.Commands;
using SqlMiM.Common.Core;
using SqlMiM.Common.ViewModel;
using SqlMiM.SqlServer.Core;
using SqlMiM.SqlServer.Model;
using SqlMiM.SqlServer.ViewModel;
using MvvmFoundation.Wpf;

namespace SqlMiM.SqlServer.SpaceManagement
{
    public class ViewModel : ObservableObject
    {
        private RelayCommand selectElements;
        private BackgroundWorker bgw;
        private ObservableCollection<DatabaseFilesGroupsView> spaceDatabases;

        /// <summary>
        /// 
        /// </summary>
        public ICommand SelectElements
        {
            get
            {
                if (selectElements == null)
                {
                    //selectElements = new RelayCommand(param => this.SelectUnselectReverse((SelectType)param));

                }
                return selectElements;
            }
        }

        public ObservableCollection<DatabaseFilesGroupsView> SpaceDatabases
        {
            get { 
                return spaceDatabases; }
            set { 
                spaceDatabases = value;
                this.RaisePropertyChanged("SpaceDatabases");
            }
        }

        public ViewModel()
        {

            SpaceDatabases = new ObservableCollection<DatabaseFilesGroupsView>();

            Load();
        }

        /// <summary>
        /// Load Components
        /// </summary>
        public void Load()
        {
            this.SpaceDatabases.Clear();

            bgw = new BackgroundWorker();
            bgw.WorkerReportsProgress = true;

            bgw.DoWork += (sender, args) => { internalGetSpaces(); };

            bgw.ProgressChanged += (sender, args) =>
            {
                if (args.UserState == null) return;

                this.SpaceDatabases.Add((DatabaseFilesGroupsView)args.UserState);
            };


            bgw.RunWorkerAsync();
        }

        private void internalGetSpaces()
        {
            var dbs = DatabasesService.Current.GetDatabases(false);

            if (dbs == null) return;

            foreach (var db in dbs)
            {
                var lstGroups = SpaceManagementServices.Current.GetFileGroupsStatistics(db);

                if (lstGroups != null)
                {
                    DatabaseFilesGroupsView dfgv = new DatabaseFilesGroupsView();
                    dfgv.Database = db;
                  
                    foreach (var fg in lstGroups)
                        dfgv.FileGroupsView.Add(new FileGroupView(fg));

                    bgw.ReportProgress(0, dfgv);
                        
                }
            }

        }


    }


    
}
